<template>
	<view class="page-box">
		<view class="order" v-for="(item, index) in list" :key="item.goodsId">
			<view class="top">
				<view class="left">
					<!-- <u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon> -->
					<view class="store">{{ item.goodsName }}</view>
					<!-- <u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon> -->
				</view>
				<view v-if="item.type == '0'" class="right">闲置</view>
				<view style="color: #287041;" v-if="item.type == '1'" class="right">求购</view>
			</view>
			<view class="item">
				<view class="left">
					<image :src="item.image.split(',')[0]" mode="aspectFill"></image>
				</view>
				<view class="content">
					<view class="title u-line-2">{{ item.goodsDesc }}</view>
					<view class="delivery-time">发布时间 {{ item.createTime }}</view>
				</view>
				<view class="right">
					<view class="price">
						￥{{ item.goodsPrice }}
					</view>
					<view class="number">x1</view>
				</view>
			</view>
			<view class="bottom">
				<view @click="toLook(item)" class="logistics btn">查看</view>
				<view @click="cancelBtn(item)" class="evaluate btn">删除订单</view>
			</view>
		</view>
		<!-- 取消收藏 -->
		<u-popup :mask-close-able="false" border-radius="15" width="70%" height="120px" v-model="show" mode="center">
			<view style="padding: 20px 15px 20px 15px;">
				确定删除订单吗?
			</view>
			<view class="conBtn">
				<u-button @click="cancel" style="margin-right: 15px;" type="info">取消</u-button>
				<u-button @click="confirm" style="margin-left: 15px;" :custom-style="customStyle" type="error">确定</u-button>
			</view>
		</u-popup>
		<u-loadmore :status="status" @loadmore="loadmore" bgColor="#f2f2f2"></u-loadmore>
	</view>
</template>

<script setup>
import { onReachBottom, onReady } from '@dcloudio/uni-app';
import { reactive, ref } from 'vue';
import {getMyOrderApi,deleteOrderApi} from '../../api/order.js'
const show = ref(false)
const list = ref([])
//列表参数
const parm = reactive({
	userId:uni.getStorageSync("userId"),
	currentPage:1,
	pageSize:10
})
const status = ref('loadmore')
//总页数
const pages = ref(0)
//获取列表数据
const getMyCollect = async()=>{
	let res = await getMyOrderApi(parm)
	if(res && res.code == 200){
		console.log(res)
		if(res.data.records.length == 0){
			status.value = "nomore"
		}
		pages.value = res.data.pages;
		list.value = list.value.concat(res.data.records)
	}
}

//加载更多
const loadmore = ()=>{
	console.log('点击加载更多')
	if(parm.currentPage >= pages.value){
		status.value = "nomore";
		return;
	}
	status.value = "loading"
	parm.currentPage = ++parm.currentPage;
	getMyCollect()
}
//触底加载
onReachBottom(()=>{
	console.log('触底加载')
	if(parm.currentPage >= pages.value){
		status.value = "nomore";
		return;
	}
	status.value = "loading"
	parm.currentPage = ++parm.currentPage;
	getMyCollect()
})
//查看
const toLook = (item)=>{
	if(item.type == '0'){//跳转闲置
		uni.navigateTo({
			url:"../look_unused/look_unused?goods=" + JSON.stringify(item)
		})
	}else{//跳转求购
		uni.navigateTo({
			url:"../look_buy/look_buy?goods=" + JSON.stringify(item)
		})
	}
}
//取消收藏按钮
const orderId = ref('')
const cancelBtn = (item)=>{
	console.log(item)
	orderId.value = item.orderId
	show.value = true;
}
//确定删除
const confirm = async()=>{
	console.log(orderId.value)
	let res = await deleteOrderApi({
		orderId:orderId.value
	})
	if(res && res.code == 200){
		parm.currentPage = 1;
		list.value = []
		getMyCollect()
		show.value = false;
	}
}
//取消
const cancel = ()=>{
	show.value = false;
}
onReady(()=>{
	getMyCollect()
})
</script>

<style lang="scss">
.conBtn{
		display: flex;
		justify-content: center;
		align-items: center;
	}
.order {
	width: 710rpx;
	background-color: #f4f4f4;
	margin: 20rpx auto;
	border-radius: 20rpx;
	box-sizing: border-box;
	padding: 20rpx;
	font-size: 28rpx;
	.top {
		display: flex;
		justify-content: space-between;
		.left {
			display: flex;
			align-items: center;
			.store {
				margin: 0 10rpx;
				font-size: 20px;
				font-weight: bold;
				letter-spacing: .1rem;
			}
		}
		.right {
			padding: 10px;
			font-size: 18px;
			color: #287041;
			letter-spacing: .1rem;
		}
	}
	.item {
		display: flex;
		margin: 20rpx 0 0;
		.left {
			margin-right: 20rpx;
			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}
		}
		.content {
			.title {
				font-size: 18px;
				line-height: 50rpx;
				padding: 5px;
			}
			.type {
				margin: 10rpx 0;
				font-size: 14px;
				color: #A9A9A9;
			}
			.delivery-time {
				color: #A9A9A9;
				font-size: 14px;
			}
		}
		.right {
			margin-left: 40px;
			padding-top: 20rpx;
			text-align: right;
			.price {
				color: #BF242A;
				font-weight: bold;
				font-size: 24px;
			}
			.number {
				color: $u-tips-color;
				font-size: 18px;
			}
		}
	}
	.total {
		margin-top: 20rpx;
		text-align: right;
		font-size: 24rpx;
		.total-price {
			font-size: 32rpx;
		}
	}
	.bottom {
		display: flex;
		margin-top: 40rpx;
		padding: 0 10rpx;
		justify-content: flex-end;
		align-items: center;
		.btn {
			line-height: 50rpx;
			width: auto;
			border-radius: 12px;
			letter-spacing: .1rem;
			border: 2px solid #A9A9A9;
			font-size: 18px;
			text-align: center;
			color: black;
			margin-right: 10px;
			padding: 5px;
		}
		.evaluate {
			color: #287041;
			border-color: #287041;
		}
	}
}
</style>
